{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 列表和元祖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 列表元组基础\n",
    "\n",
    "- 列表是动态： 长度大小不固定、可以随意添加、删减或改变元素(mutable)\n",
    "    + \n",
    "- 元祖是静态： 长度大小不固定、无法增加删减或改变(immutable)\n",
    "    + 元组新增加元素 只能创建新的元素，开辟新的内存空间\n",
    "\n",
    "- 列表和元素共性\n",
    "    + 都支持负数索引\n",
    "    + 都支持切片操作\n",
    "    + 可以随意嵌套\n",
    "    + list & tuple 函数互相转换\n",
    "\n",
    "- 列表和元组内置函数\n",
    "    + count\n",
    "    + index\n",
    "    + reverse\n",
    "    + sort\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 40, 4, 5, 'hello', True]\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "# 列表可变\n",
    "l = [1, 2, 3, 4, 5,\"hello\",True] # 列表中可以存储不同类型的数据 int float bool str\n",
    "l[2] = 40\n",
    "print(l)\n",
    "# 元祖不可变\n",
    "tup = (\"jason\",\"eric\",18) # 元祖中只能存储相同类型的数据 int float bool str\n",
    "# tup[2] = 20 # 元祖中的元素不能被修改\n",
    "tup\n",
    "newtup = tup + (\"lili\",20)\n",
    "newtup\n",
    "\n",
    "\n",
    "print(newtup[-1])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表和元素共性\n",
    "    + 都支持负数索引\n",
    "    + 都支持切片操作\n",
    "    + 可以随意嵌套\n",
    "    + list & tuple 函数互相转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "18\n",
      "[40, 5]\n",
      "('eric', 18)\n",
      "('hello', 40, 5, 4)\n",
      "['jason', 'eric', 18]\n"
     ]
    }
   ],
   "source": [
    "print(l[-1])\n",
    "print(tup[-1])\n",
    "print(l[1:3])\n",
    "print(tup[1:3])\n",
    "print(tuple(l))\n",
    "print(list(tup))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表和元组内置函数\n",
    "    + count(item): 表示统计列表/元组中item出现的次数。\n",
    "    + index(item): 表示返回列表/元组中item第一次出现的索引\n",
    "    + reverse & sort 列表和元组进行倒转和排序，reversed()返回一个倒转后的迭代器\n",
    "    + reversed() & sorted： 元组没有内置函数，通过这两个函数实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "[True, 'hello', 5, 4, 3, 2, 1]\n",
      "[5, 4, 3, 2, 1]\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5,\"hello\",True]\n",
    "print(l.count(True))\n",
    "print(l.index(4))\n",
    "l.reverse() # 返回一个倒转后的迭代器\n",
    "print(l)\n",
    "l.reverse()\n",
    "l.pop() # 返回最后一个元素，并从列表中删除\n",
    "l.pop()\n",
    "l.reverse()\n",
    "print(l)\n",
    "l.sort() # 返回一个排序后的列表\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 5, 6, 8, 9]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = (1,3,6,4,5,9,8)\n",
    "tup.count(4)\n",
    "tup.index(6)\n",
    "list(reversed(tup))\n",
    "sorted(tup)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 列表和元祖存储方式差异\n",
    "\n",
    "- 列表空间分配\n",
    "    + 列表每次新增4单位元素空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n",
      "72\n",
      "72\n",
      "72\n",
      "72\n",
      "104\n",
      "72\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "print(l.__sizeof__())\n",
    "l.append(1)\n",
    "print(l.__sizeof__())\n",
    "l.append(2.0)\n",
    "print(l.__sizeof__())\n",
    "l.append(\"hello\")\n",
    "print(l.__sizeof__())\n",
    "l.append(4)\n",
    "print(l.__sizeof__()) \n",
    "l.append(5)           # 列表每次新增4单位元素空间\n",
    "print(l.__sizeof__())\n",
    "\n",
    "l = [1,2,3,4]\n",
    "print(l.__sizeof__())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "56"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = ()\n",
    "print(tup.__sizeof__())\n",
    "tup = (1,2,3,4)\n",
    "tup.__sizeof__()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 列表和元素的性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20000000 loops, best of 5: 11 nsec per loop\n",
      "20000000 loops, best of 5: 10.8 nsec per loop\n"
     ]
    }
   ],
   "source": [
    "import timeit\n",
    "! python -m timeit 'x=(1,2,3,4,5,6)'\n",
    "\n",
    "! python -m timeit 'x=[1,2,3,4,5,6]'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20000000 loops, best of 5: 11 nsec per loop\n",
      "20000000 loops, best of 5: 11.3 nsec per loop\n"
     ]
    }
   ],
   "source": [
    "! python -m timeit -s 'x=[1,2,3,4,5,6]' 'y=x[3]'\n",
    " \n",
    "! python -m timeit -s 'x=(1,2,3,4,5,6)' 'y=x[3]'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 列表和元组使用场景\n",
    "\n",
    "- 存储数据和数量不变，适用元组\n",
    "- 存储的数据和数量可变，适用列表"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
